package cn.iocoder.yudao.module.ent.service.receivedata;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.ent.controller.admin.receivedata.vo.ReceiveDataPageReqVO;
import cn.iocoder.yudao.module.ent.controller.admin.receivedata.vo.ReceiveDataSaveReqVO;
import cn.iocoder.yudao.module.ent.dal.dataobject.receivedata.ReceiveDataDO;
import cn.iocoder.yudao.module.ent.dal.mysql.receivedata.ReceiveDataMapper;
import cn.iocoder.yudao.module.ent.enums.EntConstants;
import cn.iocoder.yudao.module.ent.enums.EntFollowType;
import cn.iocoder.yudao.module.ent.mq.producer.follow.FollowProducer;
import cn.iocoder.yudao.module.ent.service.receivenumber.ReceiveNumberService;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.ent.enums.ErrorCodeConstants.RECEIVE_DATA_NOT_EXISTS;

/**
 * 领取列表 Service 实现类
 *
 * @author PetePower
 */
@Service
@Validated
public class ReceiveDataServiceImpl implements ReceiveDataService {

    @Resource
    private ReceiveDataMapper receiveDataMapper;


    @Resource
    private ReceiveNumberService receiveNumberService;

    @Resource
    private FollowProducer followProducer;

    @Override
    public Long createReceiveData(ReceiveDataSaveReqVO createReqVO) {
        // 插入
        ReceiveDataDO receiveData = BeanUtils.toBean(createReqVO, ReceiveDataDO.class);
        receiveDataMapper.insert(receiveData);
        // 返回
        return receiveData.getId();
    }

    @Override
    public Long batchCreateReceiveData(List<ReceiveDataSaveReqVO> createReqVO) {
        receiveDataMapper.insertBatch(BeanUtils.toBean(createReqVO, ReceiveDataDO.class));
        return 1L;
    }

    @Override
    public void updateReceiveData(ReceiveDataSaveReqVO updateReqVO) {
        // 校验存在
        validateReceiveDataExistsAndReturn(updateReqVO.getId());
        // 更新
        ReceiveDataDO updateObj = BeanUtils.toBean(updateReqVO, ReceiveDataDO.class);
        receiveDataMapper.updateById(updateObj);
    }

    @Override
    public void updateReceiveDataStatus(String entId, String type, String content, LocalDateTime time) {
        receiveDataMapper.update(new LambdaUpdateWrapper<ReceiveDataDO>()
                .set(ReceiveDataDO::getFollowStatus, true)
                .set(ReceiveDataDO::getLastFollowType, type)
                .set(ReceiveDataDO::getLastFollowRecord, content)
                .set(ReceiveDataDO::getLastFollowTime, time)
                .eq(ReceiveDataDO::getEntId, entId));
    }

    @Override
    public void deleteReceiveData(Long id) {
        // 校验存在
        ReceiveDataDO receiveDataDO = validateReceiveDataExistsAndReturn(id);
        // 删除
        receiveDataMapper.deleteById(id);

        // 同时删除掉号码列表 并且创建一个放回公海的跟进记录
        receiveNumberService.batchDeleteReceiveNumberByEntId(receiveDataDO.getEntId());

        followProducer.sendReceiveMessage(EntFollowType.DELETE.getType(), receiveDataDO.getOwnerUserId(), receiveDataDO.getEntId(), null, EntConstants.DELETE_FOLLOW_RECORD, null, null, null);

    }

    @Override
    public void batchDeleteReceiveData(List<Long> idList) {
        idList.forEach(this::deleteReceiveData);
    }

    private ReceiveDataDO validateReceiveDataExistsAndReturn(Long id) {
        ReceiveDataDO receiveDataDO = receiveDataMapper.selectById(id);
        if (receiveDataDO == null) {
            throw exception(RECEIVE_DATA_NOT_EXISTS);
        }
        return receiveDataDO;
    }

    @Override
    public ReceiveDataDO getReceiveData(Long id) {
        return receiveDataMapper.selectById(id);
    }

    @Override
    public ReceiveDataDO getReceiveData(String cid) {
        return receiveDataMapper.selectOne(new LambdaQueryWrapperX<ReceiveDataDO>().eq(ReceiveDataDO::getEntId, cid), false);
    }

    @Override
    public PageResult<ReceiveDataDO> getReceiveDataPage(ReceiveDataPageReqVO pageReqVO) {

        return receiveDataMapper.selectPage(pageReqVO);
    }

    @Override
    public Long countBtTime(ReceiveDataPageReqVO pageReqVO) {
        return receiveDataMapper.selectCount(new LambdaQueryWrapperX<ReceiveDataDO>().eqIfPresent(ReceiveDataDO::getFollowStatus, pageReqVO.getFollowStatus()).betweenIfPresent(ReceiveDataDO::getCreateTime, pageReqVO.getCreateTime()));
    }
}